package com.epower.dao.epowersentence.impl;

import java.sql.SQLException;
import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Component;
import com.ali.luna.commons.dao.BaseDAOImpl;
import com.epower.dao.epowersentence.EpowerSentenceDAO;
import com.epower.dataobject.EpowerSentenceDO;
import com.ibatis.sqlmap.client.SqlMapExecutor;

@Scope("singleton")
@Component("epowerSentenceDAO")
public class EpowerSentenceDAOImpl extends BaseDAOImpl implements EpowerSentenceDAO {

    @Override
    public Integer addEpowerSentence(final List<EpowerSentenceDO> epowerSentenceDOs) {
        return (Integer) this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                for (EpowerSentenceDO sentencedo : epowerSentenceDOs) {
                    executor.insert("EpowerSentenceDAOImpl.ADD-EPOWERSENTENCE", sentencedo);
                }
                return executor.executeBatch();
            }
        });
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<EpowerSentenceDO> querySentenceBySpelling(String spelling) {
        return getSqlMapClientTemplate().queryForList("EpowerSentenceDAOImpl.GET-EPOWERSENTENCE-BY-SPELLING",spelling);
    }

    @Override
    public int querySentenceCountBySpelling(String spelling) {
        return (Integer) getSqlMapClientTemplate().queryForObject("EpowerSentenceDAOImpl.GET-EPOWERSENTENCE-COUNT-BY-SPELLING",spelling);
    }

    @Override
    public int querySentenceById(int id) {
        return (Integer) getSqlMapClientTemplate().queryForObject("EpowerSentenceDAOImpl.GET-EPOWERSENTENCE-BY-ID",id);
    }

    @Override
    public int updateEpowerSentence(final List<EpowerSentenceDO> epowerSentenceDOs) {
        return (Integer) this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                for (EpowerSentenceDO sentencedo : epowerSentenceDOs) {
                    executor.update("EpowerSentenceDAOImpl.UPDATE-EPOWERSENTENCE-BY-OPTION", sentencedo);
                }
                return executor.executeBatch();
            }
        });
    }

    @Override
    public Integer deleteEpowerSentenceBySpelling(String spelling) {
        return getSqlMapClientTemplate().update("EpowerSentenceDAOImpl.DELETE-EPOWERSENTENCE-BY-SPELLING", spelling);
    }

    @Override
    public boolean deleteEpowerSentenceById(int id) {
        int num = getSqlMapClientTemplate().update("EpowerSentenceDAOImpl.DELETE-EPOWERSENTENCE-BY-ID", id);
        return num == 0 ? false : true;
    }
}
